package com.xinzhi.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinzhi.model.Recruiting_plan;
import com.xinzhi.model.Result;
import com.xinzhi.service.IRecruiting_planService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class Recruiting_planController {

    @Autowired
    private IRecruiting_planService iRecruiting_planService;


    @RequestMapping("/addplan")
    @ResponseBody
    public Result addplan(Recruiting_plan recruiting_plan) {
        int addplan = iRecruiting_planService.addplan(recruiting_plan);
        if (addplan > 0) {
            return Result.ok();
        }
        return Result.error(404, "添加失败1");
    }

    @RequestMapping("/deleteplan")
    @ResponseBody
    public Result deleteplan(Recruiting_plan recruiting_plan) {
        int deleteplan = iRecruiting_planService.deleteplan(recruiting_plan);
        if (deleteplan > 0) {
            return Result.ok();
        }
        return Result.error(404, "添加失败");
    }

    @RequestMapping("/updateplan")
    @ResponseBody
    public Result updateplan(Recruiting_plan recruiting_plan) {
//        recruiting_plan.setPartName(recruiting_plan.getPartName());
//        recruiting_plan.setDiploma(recruiting_plan.getDiploma());
//        recruiting_plan.setNum(recruiting_plan.getNum());
        int updateplan = iRecruiting_planService.updateplan(recruiting_plan);
        if (updateplan > 0) {
            return Result.ok(updateplan);
        }
        return Result.error(999, "失败");
    }

    @RequestMapping("/allplan")
    @ResponseBody
    public Result allplan(Recruiting_plan recruiting_plan, int page) {
        if (page > 0) {
            page = page * (page - 1);
        } else {
            page = -1;
        }
        PageHelper.startPage(page, recruiting_plan.getSize());
        List<Recruiting_plan> findall = iRecruiting_planService.findAll(recruiting_plan, page);

        PageInfo<Recruiting_plan> pageInfo = new PageInfo<>(findall);

        int pages = pageInfo.getPages();

        if (pages < page || page == -1) {
            return Result.error("404");
        }
        List<Recruiting_plan> list = pageInfo.getList();
        for (Recruiting_plan recruiting_plan1 : list) {
            System.out.println(recruiting_plan1);
        }
        return Result.ok();
    }

    @RequestMapping(value = "/allplan", method = RequestMethod.GET)
    @ResponseBody
    public Result allplan(Recruiting_plan recruiting_plan) {
        List<Recruiting_plan> all = iRecruiting_planService.findAll(recruiting_plan);
        if (all.size() != 0) {
            return Result.ok(all);
        }
        return Result.error(999, "失败");
    }

    @RequestMapping("/fuzzyplan")
    @ResponseBody
    public Result fuzzyplan(Recruiting_plan recruiting_plan) {
        List<Recruiting_plan> fuzzyplan = iRecruiting_planService.fuzzyplan(recruiting_plan);
        if (fuzzyplan.size() != 0) {
            return Result.ok(fuzzyplan);
        }
        return Result.error(999, "失败");
    }

}